// pages/softProject/softProject.js
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    button_steps:['','创建','创建','创建','创建','创建','创建','创建','创建','创建','创建','创建','创建','创建'],
    title: '',
    version: '',
    author_name: '',
    project_id:'',
  },

  checkPartFinish: function(){
    var noFinishStepName = null;

    var docItemKey = 'pid_'+this.data.project_id
    var stableDocData = wx.getStorageSync(docItemKey)

    var docJson = JSON.parse(stableDocData)

    for(var i=1;i<=13;i++){
        var jsonItemName = 'step'+i+'Data'
        if(docJson[jsonItemName] === undefined || docJson[jsonItemName] == null){
            var names = ['','简介','产品范围','产品概述','软件架构','软件编译环境','软件运行效果',
                '软件安装说明','使用步骤1','使用步骤2','使用步骤3','使用步骤4','使用步骤5','附加功能1']
            wx.showModal({
              title: '错误信息',
              content: '请填写：Part '+names[i]+'',
              showCancel: false
            });
            return false;
            
        }

    }
    return true;
},

  submitTaskProcess(){
    var that = this
    if(!that.checkPartFinish()){
      return;
    }
    var docItemKey = 'pid_'+this.data.project_id
    var stableDocData = wx.getStorageSync(docItemKey)
    wx.request({
      //测试时
      //url: 'https://register-pub.com/submit_soft_task_test.php',
      url: 'https://register-pub.com/submit_soft_task.php',
      header:{
        'Cookie': wx.getStorageSync('cookie')
      },
      method: 'POST',
      data: stableDocData,
      success: function(res) {
        console.log(res.data)
        if (res.data.code == 0) {
          wx.hideLoading();
          //测试时不删除key
          wx.removeStorageSync('pid_'+that.data.project_id)
          wx.showModal({
            title: '提交成功',
            content: '任务提交成功，本地记录已删除，请在[我的]页面的[任务列表]查看对应远程任务状态',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack()
              }
            }
          });
        } else {
          wx.hideLoading();
          var showTitle = res.data.message && res.data.message!=''?res.data.message:'网络错误';
          util.showErrorToast('错误:'+showTitle)
        }
      },
      fail: function(){
        util.showErrorToast('错误:网络出错')
      }
    });
  },

  submitTask(){
    wx.showLoading({
      title: "加载中",
      mask: true  //开启蒙版遮罩
    });

    var localCookie = wx.getStorageSync('cookie')
    var that = this
    if(!localCookie){
      console.log('----------no cookie')
      wx.hideLoading();
      wx.navigateTo({
        url: "/pages/auth/accountLogin/accountLogin?action=softProject"
      });
      return;
    }

    var header = {
      'Cookie':localCookie
    }

    wx.request({
      url: 'https://register-pub.com/use_info.php',
      header:header,
      method: 'GET',
      
      success: function(res) {
        console.log(res.data)
        if (res.data.code == 0 && res.data.data && res.data.data.is_login && res.data.data.is_login==true) {
          wx.hideLoading();
          
          that.submitTaskProcess()
        } else {
          wx.hideLoading();
          wx.navigateTo({
            url: "/pages/auth/accountLogin/accountLogin?action=softProject"
          });
        }
      },
      fail: function(){
        util.showErrorToast('错误:网络出错')
      }
    });


    

  },

  toStep(e){
    const index= e.currentTarget.dataset['index'];
    console.log('----------'+index)
    var url = '/pages/softStep'+index+'/softStep'+index+'?project_id='+this.data.project_id+"&title="+this.data.title
    wx.navigateTo({
      url: url
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.title){
      this.setData({
        title: options.title
      })
      
    }
    if (options.version){
      this.setData({
        version: options.version
      })
    }
    if (options.author_name){
      this.setData({
        author_name: options.author_name
      })
    }
    if (options.project_id){
      this.setData({
        project_id: options.project_id
      })
      
    }
    console.log(this.data.title)
    
  },

  onInitPage(){
    var pid = this.data.project_id
    var docItemKey = 'pid_'+pid
    var stableDocData = wx.getStorageSync(docItemKey)
    console.log(stableDocData)
    if(stableDocData){
        var docJson = JSON.parse(stableDocData)
        this.setData({
          title: docJson.title
        })
    }
    var docJson = {};

    docJson.title = this.data.title
    docJson.version = this.data.version
    docJson.author_name = this.data.author_name
    docJson.project_id = this.data.project_id
    docJson.create_time = new Date().getTime()
    docJson.edit_time = new Date().getTime()
    var docItemKey = 'pid_'+this.data.project_id
    var stableDocData = wx.getStorageSync(docItemKey)
    if(!stableDocData){
        wx.setStorageSync(docItemKey,JSON.stringify(docJson))
    }else {
        docJson = JSON.parse(stableDocData)
        docJson.edit_time = new Date().getTime()
        wx.setStorageSync(docItemKey,JSON.stringify(docJson))
    }
    if(docJson.step1Data !==undefined && docJson.step1Data!=null){
      this.data.button_steps[1] = '编辑'
    }
    if(docJson.step2Data !==undefined && docJson.step2Data!=null){
      this.data.button_steps[2] = '编辑'
    }
    if(docJson.step3Data !==undefined && docJson.step3Data!=null){
      this.data.button_steps[3] = '编辑'
    }
    if(docJson.step4Data !==undefined && docJson.step4Data!=null){
      this.data.button_steps[4] = '编辑'
    }
    if(docJson.step5Data !==undefined && docJson.step5Data!=null){
      this.data.button_steps[5] = '编辑'
    }
    if(docJson.step6Data !==undefined && docJson.step6Data!=null){
      this.data.button_steps[6] = '编辑'
    }
    if(docJson.step7Data !==undefined && docJson.step7Data!=null){
      this.data.button_steps[7] = '编辑'
    }
    if(docJson.step8Data !==undefined && docJson.step8Data!=null){
      this.data.button_steps[8] = '编辑'
    }
    if(docJson.step9Data !==undefined && docJson.step9Data!=null){
      this.data.button_steps[9] = '编辑'
    }
    if(docJson.step10Data !==undefined && docJson.step10Data!=null){
      this.data.button_steps[10] = '编辑'
    }
    if(docJson.step11Data !==undefined && docJson.step11Data!=null){
      this.data.button_steps[11] = '编辑'
    }
    if(docJson.step12Data !==undefined && docJson.step12Data!=null){
      this.data.button_steps[12] = '编辑'
    }
    if(docJson.step13Data !==undefined && docJson.step13Data!=null){
      this.data.button_steps[13] = '编辑'
    }

    var that = this
    this.setData({
      button_steps: that.data.button_steps
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.onInitPage()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})